home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume13 / gmcalc / part19 < prev    next >
Encoding:
Text File  |  1990-06-05  |  52.7 KB  |  1,250 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@csvax.caltech.edu (David Gillespie)
  3. Subject: v13i045: Emacs Calculator 1.01, part 19/19
  4. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5.  
  6. Posting-number: Volume 13, Issue 45
  7. Submitted-by: daveg@csvax.caltech.edu (David Gillespie)
  8. Archive-name: gmcalc/part19
  9.  
  10. ---- Cut Here and unpack ----
  11. #!/bin/sh
  12. # this is part 19 of a multipart archive
  13. # do not concatenate these parts, unpack them in order with /bin/sh
  14. # file calc.texinfo continued
  15. #
  16. CurArch=19
  17. if test ! -r s2_seq_.tmp
  18. then echo "Please unpack part 1 first!"
  19.      exit 1; fi
  20. ( read Scheck
  21.   if test "$Scheck" != $CurArch
  22.   then echo "Please unpack part $Scheck next!"
  23.        exit 1;
  24.   else exit 0; fi
  25. ) < s2_seq_.tmp || exit 1
  26. echo "x - Continuing file calc.texinfo"
  27. sed 's/^X//' << 'SHAR_EOF' >> calc.texinfo
  28. Xdue to roundoff error.
  29. X@end defun
  30. X
  31. X@defun is-true x
  32. XReturn true if the formula @var{x} represents a true value in
  33. XCalc, not Lisp, terms.  It tests if @var{x} is a non-zero number.
  34. X@end defun
  35. X
  36. X@defun reject-arg val pred
  37. XAbort the current function evaluation due to unacceptable argument values.
  38. XThis calls @samp{(calc-record-why @var{pred} @var{val})}, then signals a
  39. XLisp error which @code{normalize} will trap.  The net effect is that the
  40. Xfunction call which led here will be left in symbolic form.@refill
  41. X@end defun
  42. X
  43. X@defun inexact-value
  44. XIf Symbolic Mode is enabled, this will signal an error that causes
  45. X@code{normalize} to leave the formula in symbolic form, with the message
  46. X``Inexact result.''  (This function has no effect when not in Symbolic Mode.)
  47. XNote that if your function calls @samp{(sin 5)} in Symbolic Mode, the
  48. X@code{sin} function will call @code{inexact-value}, which will cause your
  49. Xfunction to be left unsimplified.  You may instead wish to call
  50. X@samp{(normalize (list 'calcFunc-sin 5))}, which in Symbolic Mode will
  51. Xreturn the formula @samp{sin(5)} to your function.@refill
  52. X@end defun
  53. X
  54. X@node Computational Lisp Functions, Vector Lisp Functions, Predicates, Internals
  55. X@subsubsection Computational Functions
  56. X
  57. XThe functions described here do the actual computational work of the
  58. XCalculator.  In addition to these, note that any function described in
  59. Xthe main body of this manual may be called from Lisp; for example, if
  60. Xthe documentation refers to the @code{calc-sqrt} [@code{sqrt}] command,
  61. Xthis means @code{calc-sqrt} is an interactive stack-based square-root
  62. Xcommand and @code{sqrt} (which @code{defmath} expands to @code{calcFunc-sqrt})
  63. Xis the actual Lisp function for taking square roots.@refill
  64. X
  65. XThe functions @code{math-add}, @code{math-sub}, @code{math-mul},
  66. X@code{math-div}, @code{math-mod}, and @code{math-neg} are not included
  67. Xin this list, since @code{defmath} allows you to write native Lisp
  68. X@code{+}, @code{-}, @code{*}, @code{/}, @code{%}, and unary @code{-},
  69. Xrespectively, instead.@refill
  70. X
  71. X@defun normalize val
  72. X(Full form: @code{math-normalize}.)
  73. XReduce the value @var{val} to standard form.  For example, if @var{val}
  74. Xis a fixnum, it will be converted to a bignum if it is too large, and
  75. Xif @var{val} is a bignum it will be normalized by clipping off trailing
  76. X(i.e., most-significant) zero digits and converting to a fixnum if it is
  77. Xsmall.  All the various data types are similarly converted to their standard
  78. Xforms.  Variables are left alone, but function calls are actually evaluated
  79. Xin formulas.  For example, normalizing @samp{(+ 2 (calcFunc-abs -4))} will
  80. Xreturn 6.@refill
  81. X
  82. XIf a function call fails, because the function is void or has the wrong
  83. Xnumber of parameters, or because it returns @code{nil} or calls
  84. X@code{reject-arg} or @code{inexact-result}, @code{normalize} returns
  85. Xthe formula still in symbolic form.@refill
  86. X
  87. XIf the current Simplification Mode is ``none'' or ``numeric arguments
  88. Xonly,'' function calls may not be normalized.  However, the more
  89. Xpowerful simplification modes (like algebraic simplification) are
  90. Xnot handled by @code{normalize}.  They are handled by @code{calc-normalize},
  91. Xwhich calls @code{normalize} and possibly some other routines, such
  92. Xas @code{simplify} or @code{simplify-units}.  Programs should never
  93. Xcall @code{calc-normalize} except when popping or pushing values on
  94. Xthe stack.@refill
  95. X@end defun
  96. X
  97. X@defun evaluate-expr expr
  98. XReplace all variables in @var{expr} that have values with their values,
  99. Xthen use @code{normalize} to simplify the result.  This is what happens
  100. Xwhen you press the @kbd{=} key interactively.@refill
  101. X@end defun
  102. X
  103. X@defmac with-extra-prec n body
  104. XEvaluate the Lisp forms in @var{body} with precision increased by @var{n}
  105. Xdigits.  This is a macro which expands to
  106. X
  107. X@example
  108. X(math-normalize
  109. X  (let ((calc-internal-prec (+ calc-internal-prec @var{n})))
  110. X    @var{body}))
  111. X@end example
  112. X
  113. XThe surrounding call to @code{math-normalize} causes a floating-point
  114. Xresult to be rounded down to the original precision afterwards.  This
  115. Xis important because some arithmetic operations assume a number's
  116. Xmantissa contains no more digits than the current precision allows.
  117. X@end defmac
  118. X
  119. X@defun make-frac n d
  120. XBuild a fraction @samp{@var{n}:@var{d}}.  This is equivalent to calling
  121. X@samp{(normalize (list 'frac @var{n} @var{d}))}, but more efficient.
  122. X@end defun
  123. X
  124. X@defun make-float mant exp
  125. XBuild a floating-point value out of @var{mant} and @var{exp}.
  126. X@end defun
  127. X
  128. X@defun make-sdev x sigma
  129. XBuild an error form out of @var{x} and the absolute value of @var{sigma}.
  130. XIf @var{sigma} is zero, the result is the number @var{x} directly.
  131. XIf @var{x} or @var{sigma} is not a valid type of object for use in
  132. Xerror forms, this calls @code{reject-arg}.
  133. X@end defun
  134. X
  135. X@defun make-intv mask lo hi
  136. XBuild an interval form out of @var{mask} (which is assumed to be an
  137. Xinteger from 0 to 3), and the limits @var{lo} and @var{hi}.  If
  138. X@var{lo} is greater than @var{hi}, an empty interval form is returned.
  139. XThis calls @code{reject-arg} if @var{lo} or @var{hi} is unsuitable.
  140. X@end defun
  141. X
  142. X@defun sort-intv mask lo hi
  143. XBuild an interval form, similar to @code{make-intv}, except that if
  144. X@var{lo} is less than @var{hi} they are simply exchanged, and the
  145. Xbits of @var{mask} are swapped accordingly.
  146. X@end defun
  147. X
  148. X@defun make-mod n m
  149. X@tindex makemod
  150. XBuild a modulo form out of @var{n} and the modulus @var{m}.  Since modulo
  151. Xforms do not allow formulas as their components, if @var{n} or @var{m}
  152. Xis not a real number or HMS form the result will be a formula which
  153. Xis a call to @code{makemod}, the algebraic version of this function.
  154. X@end defun
  155. X
  156. X@defun float x
  157. XConvert @var{x} to floating-point form.  Integers and fractions are
  158. Xconverted to numerically equivalent floats; components of complex
  159. Xnumbers, vectors, HMS forms, error forms, intervals, and modulo forms
  160. Xare recursively floated.  If the argument is a variable or formula,
  161. Xthis calls @code{reject-arg}.
  162. X@end defun
  163. X
  164. X@defun compare x y
  165. XCompare the numbers @var{x} and @var{y}, and return -1 if
  166. X@samp{(lessp @var{x} @var{y})}, 1 if @samp{(lessp @var{y} @var{x})},
  167. X0 if @samp{(math-equal @var{x} @var{y})}, or 2 if the order is
  168. Xundefined or cannot be determined.@refill
  169. X@end defun
  170. X
  171. X@defun numdigs n
  172. XReturn the number of digits of integer @var{n}, effectively
  173. X@samp{ceil(log10(@var{n}))}, but much more efficient.  Zero is
  174. Xconsidered to have zero digits.
  175. X@end defun
  176. X
  177. X@defun scale-int x n
  178. XShift integer @var{x} left @var{n} digits, or right -@var{n} digits
  179. Xwith truncation toward zero.
  180. X@end defun
  181. X
  182. X@defun scale-rounding x n
  183. XLike @code{scale-int}, except that a right shift rounds to the nearest
  184. Xinteger rather than truncating.
  185. X@end defun
  186. X
  187. X@defun fixnum n
  188. XReturn the integer @var{n} as a fixnum, i.e., a native Lisp integer.
  189. XIf @var{n} is outside the permissible range for Lisp integers (usually
  190. X24 binary bits) the result is undefined.
  191. X@end defun
  192. X
  193. X@defun sqr x
  194. XCompute the square of @var{x}; short for @samp{(^ @var{x} 2)}.
  195. X@end defun
  196. X
  197. X@defun quotient x y
  198. XDivide integer @var{x} by integer @var{y}; return an integer quotient
  199. Xand discard the remainder.  If @var{x} or @var{y} is negative, the
  200. Xdirection of rounding is undefined.
  201. X@end defun
  202. X
  203. X@defun idiv x y
  204. XPerform an integer division; if @var{x} and @var{y} are both nonnegative
  205. Xintegers, this uses @code{quotient}, otherwise it computes
  206. X@samp{floor(@var{x}/@var{y})}.  Thus the result is well-defined but
  207. Xslower than for @code{quotient}.
  208. X@end defun
  209. X
  210. X@defun imod x y
  211. XDivide integer @var{x} by integer @var{y}; return the integer remainder
  212. Xand discard the quotient.  Like @code{quotient}, this works only for
  213. Xinteger arguments and is not well-defined for negative arguments.
  214. XFor a more well-defined result, use @samp{(% @var{x} @var{y})}.
  215. X@end defun
  216. X
  217. X@defun idivmod x y
  218. XDivide integer @var{x} by integer @var{y}; return a cons cell whose
  219. X@code{car} is @samp{(quotient @var{x} @var{y})} and whose @code{cdr}
  220. Xis @samp{(imod @var{x} @var{y})}.@refill
  221. X@end defun
  222. X
  223. X@defun pow x y
  224. XCompute @var{x} to the power @var{y}.  In @code{defmath}, this can also
  225. Xbe written @samp{(^ @var{x} @var{y})} or @samp{(expt @var{x} @var{y})}.
  226. X@end defun
  227. X
  228. X@defun abs-approx x
  229. XCompute a fast approximation to the absolute value of @var{x}.  For
  230. Xexample, for a rectangular complex number the result is the sum of
  231. Xthe absolute values of the components.
  232. X@end defun
  233. X
  234. X@findex two-pi
  235. X@findex pi-over-2
  236. X@findex pi-over-4
  237. X@findex pi-over-180
  238. X@findex sqrt-two-pi
  239. X@findex sqrt-e
  240. X@findex e
  241. X@findex ln-2
  242. X@findex ln-10
  243. X@defun pi
  244. XThe function @samp{(pi)} computes @samp{pi} to the current precision.
  245. XSome other related constant-generating functions are @code{two-pi},
  246. X@code{pi-over-2}, @code{pi-over-4}, @code{pi-over-180}, @code{sqrt-two-pi},
  247. X@code{e}, @code{sqrt-e}, @code{ln-2}, and @code{ln-10}.  Each function
  248. Xreturns a floating-point value in the current precision, and each uses
  249. Xcaching so that all calls after the first are essentially free.@refill
  250. X@end defun
  251. X
  252. X@defmac math-defcache @var{func} @var{initial} @var{form}
  253. XThis macro, usually used as a top-level call like @code{defun} or
  254. X@code{defvar}, defines a new cached constant analogous to @code{pi}, etc.
  255. XIt defines a function @code{func} which returns the requested value;
  256. Xif @var{initial} is non-@code{nil} it must be a @samp{(float @dots{})}
  257. Xform which serves as an initial value for the cache.  If @var{func}
  258. Xis called when the cache is empty or does not have enough digits to
  259. Xsatisfy the current precision, the Lisp expression @var{form} is evaluated
  260. Xwith the current precision increased by four, and the result minus its
  261. Xtwo least significant digits is stored in the cache.  For example,
  262. Xcalling @samp{(pi)} with a precision of 30 computes @samp{pi} to 34
  263. Xdigits, rounds it down to 32 digits for future use, then rounds it
  264. Xagain to 30 digits for use in the present request.@refill
  265. X@end defmac
  266. X
  267. X@findex half-circle
  268. X@findex quarter-circle
  269. X@defun full-circle symb
  270. XIf the current angular mode is Degrees or HMS, this function returns the
  271. Xinteger 360.  In Radians mode, this function returns either the
  272. Xcorresponding value in radians to the current precision, or the formula
  273. X@samp{2*pi}, depending on the Symbolic Mode.  There are also similar
  274. Xfunction @code{half-circle} and @code{quarter-circle}.
  275. X@end defun
  276. X
  277. X@defun power-of-2 n
  278. XCompute two to the integer power @var{n}, as a (potentially very large)
  279. Xinteger.  Powers of two are cached, so only the first call for a
  280. Xparticular @var{n} is expensive.
  281. X@end defun
  282. X
  283. X@defun integer-log2 n
  284. XCompute the base-2 logarithm of @var{n}, which must be an integer which
  285. Xis a power of two.  If @var{n} is not a power of two, this function will
  286. Xreturn @code{nil}.
  287. X@end defun
  288. X
  289. X@defun div-mod a b m
  290. XDivide @var{a} by @var{b}, modulo @var{m}.  This returns @code{nil} if
  291. Xthere is no solution, or if any of the arguments are not integers.@refill
  292. X@end defun
  293. X
  294. X@defun pow-mod a b m
  295. XCompute @var{a} to the power @var{b}, modulo @var{m}.  If @var{a},
  296. X@var{b}, and @var{m} are integers, this uses an especially efficient
  297. Xalgorithm.  Otherwise, it simply computes @samp{(% (^ a b) m)}.
  298. X@end defun
  299. X
  300. X@defun isqrt n
  301. XCompute the integer square root of @var{n}.  This is the square root
  302. Xof @var{n} rounded down toward zero, i.e., @samp{floor(sqrt(@var{n}))}.
  303. XIf @var{n} is itself an integer, the computation is especially efficient.
  304. X@end defun
  305. X
  306. X@defun to-hms a ang
  307. XConvert the argument @var{a} into an HMS form.  If @var{ang} is specified,
  308. Xit is the angular mode in which to interpret @var{a}, either @samp{'deg}
  309. Xor @samp{'rad}.  Otherwise, the current angular mode is used.  If @var{a}
  310. Xis already an HMS form it is returned as-is.
  311. X@end defun
  312. X
  313. X@defun from-hms a ang
  314. XConvert the HMS form @var{a} into a real number.  If @var{ang} is specified,
  315. Xit is the angular mode in which to express the result, otherwise the
  316. Xcurrent angular mode is used.  If @var{a} is already a real number, it
  317. Xis returned as-is.
  318. X@end defun
  319. X
  320. X@defun to-radians a
  321. XConvert the number or HMS form @var{a} to radians from the current
  322. Xangular mode.
  323. X@end defun
  324. X
  325. X@defun from-radians a
  326. XConvert the number @var{a} from radians to the current angular mode.
  327. XIf @var{a} is a formula, this returns the formula @samp{deg(@var{a})}.
  328. X@end defun
  329. X
  330. X@defun to-radians-2 a
  331. XLike @code{to-radians}, except that in Symbolic Mode a degrees to
  332. Xradians conversion yields a formula like @samp{@var{a}*pi/180}.
  333. X@end defun
  334. X
  335. X@defun from-radians-2 a
  336. XLike @code{from-radians}, except that in Symbolic Mode a radians to
  337. Xdegrees conversion yields a formula like @samp{@var{a}*180/pi}.
  338. X@end defun
  339. X
  340. X@defun random-digit
  341. XProduce a random base-1000 digit in the range 0 to 999.
  342. X@end defun
  343. X
  344. X@defun random-digits n
  345. XProduce a random @var{n}-digit integer; this will be an integer
  346. Xin the interval @samp{[0, 10^@var{n})}.
  347. X@end defun
  348. X
  349. X@defun random-float
  350. XProduce a random float in the interval @samp{[0, 1)}.
  351. X@end defun
  352. X
  353. X@defun prime-test n iters
  354. XDetermine whether the integer @var{n} is prime.  Return a list which has
  355. Xone of these forms: @samp{(nil @var{f})} means the number is non-prime
  356. Xbecause it was found to be divisible by @var{f}; @samp{(nil)} means it
  357. Xwas found to be non-prime by table look-up (so no factors are known);
  358. X@samp{(nil unknown)} means it is definitely non-prime but no factors
  359. Xare known because @var{n} was large enough that Fermat's probabilistic
  360. Xtest had to be used; @samp{(t)} means the number is definitely prime;
  361. Xand @samp{(maybe @var{i} @var{p})} means that Fermat's test, after @var{i}
  362. Xiterations, is @var{p} percent sure that the number is prime.  The
  363. X@var{iters} parameter is the number of Fermat iterations to use, in the
  364. Xcase that this is necessary.  If @code{prime-test} returns ``maybe,''
  365. Xyou can call it again with the same @var{n} to get a greater certainty;
  366. X@code{prime-test} remembers where it left off.@refill
  367. X@end defun
  368. X
  369. X@defun to-simple-fraction f
  370. XIf @var{f} is a floating-point number which can be represented exactly
  371. Xas a small rational number. return that number, else return @var{f}.
  372. XFor example, 0.75 would be converted to 3:4.  This function is very
  373. Xfast.
  374. X@end defun
  375. X
  376. X@defun to-fraction f tol
  377. XFind a rational approximation to floating-point number @var{f} to within
  378. Xa specified tolerance @var{tol}; this corresponds to the algebraic
  379. Xfunction @code{frac}, and can be rather slow.
  380. X@end defun
  381. X
  382. X@node Vector Lisp Functions, Symbolic Lisp Functions, Computational Lisp Functions, Internals
  383. X@subsubsection Vector Functions
  384. X
  385. XThe functions described here perform various operations on vectors and
  386. Xmatrices.
  387. X
  388. X@defun math-concat x y
  389. XDo a vector concatenation; this operation is written @samp{@var{x} | @var{y}}
  390. Xin a symbolic formula.  @xref{Building Vectors}.
  391. X@end defun
  392. X
  393. X@defun vec-length v
  394. XReturn the length of vector @var{v}.  If @var{v} is not a vector, the
  395. Xresult is zero.  If @var{v} is a matrix, this returns the number of
  396. Xrows in the matrix.
  397. X@end defun
  398. X
  399. X@defun mat-dimens m
  400. XDetermine the dimensions of vector or matrix @var{m}.  If @var{m} is not
  401. Xa vector, the result is an empty list.  If @var{m} is a plain vector
  402. Xbut not a matrix, the result is a one-element list containing the length
  403. Xof the vector.  If @var{m} is a matrix with @var{r} rows and @var{c} columns,
  404. Xthe result is the list @samp{(@var{r} @var{c})}.  Higher-order tensors
  405. Xproduce lists of more than two dimensions.  Note that the object
  406. X@samp{[[1, 2, 3], [4, 5]]} is a vector of vectors not all the same size,
  407. Xand is treated by this and other Calc routines as a plain vector of two
  408. Xelements.@refill
  409. X@end defun
  410. X
  411. X@defun dimension-error
  412. XAbort the current function with a message of ``Dimension error.''
  413. XThe Calculator will leave the function being evaluated in symbolic
  414. Xform; this is really just a special case of @code{reject-arg}.
  415. X@end defun
  416. X
  417. X@defun build-vector args
  418. XReturn a Calc vector with the zero-or-more @var{args} as elements.
  419. XFor example, @samp{(build-vector 1 2 3)} returns the Calc vector
  420. X@samp{[1, 2, 3]}, stored internally as the list @samp{(vec 1 2 3)}.
  421. X@end defun
  422. X
  423. X@defun make-vec obj dims
  424. XReturn a Calc vector or matrix all of whose elements are equal to
  425. X@var{obj}.  For example, @samp{(make-vec 27 3 4)} returns a 3x4 matrix
  426. Xfilled with 27's.
  427. X@end defun
  428. X
  429. X@defun row-matrix v
  430. XIf @var{v} is a plain vector, convert it into a row matrix, i.e.,
  431. Xa matrix whose single row is @var{v}.  If @var{v} is already a matrix,
  432. Xleave it alone.
  433. X@end defun
  434. X
  435. X@defun col-matrix v
  436. XIf @var{v} is a plain vector, convert it into a column matrix, i.e., a
  437. Xmatrix with each element of @var{v} as a separate row.  If @var{v} is
  438. Xalready a matrix, leave it alone.
  439. X@end defun
  440. X
  441. X@defun map-vec f v
  442. XMap the Lisp function @var{f} over the Calc vector @var{v}.  For example,
  443. X@samp{(map-vec 'math-floor v)} returns a vector of the floored components
  444. Xof vector @var{v}.
  445. X@end defun
  446. X
  447. X@defun map-vec-2 f a b
  448. XMap the Lisp function @var{f} over the two vectors @var{a} and @var{b}.
  449. XIf @var{a} and @var{b} are vectors of equal length, the result is a
  450. Xvector of the results of calling @samp{(@var{f} @var{ai} @var{bi})}
  451. Xfor each pair of elements @var{ai} and @var{bi}.  If either @var{a} or
  452. X@var{b} is a scalar, it is matched with each value of the other vector.
  453. XFor example, @samp{(map-vec-2 'math-add v 1)} returns the vector @var{v}
  454. Xwith each element increased by one.  Note that using @samp{'+} would not
  455. Xwork here, since @code{defmath} does not expand function names everywhere,
  456. Xjust where they are in the function position of a Lisp expression.@refill
  457. X@end defun
  458. X
  459. X@defun reduce-vec f v
  460. XReduce the function @var{f} over the vector @var{v}.  For example, if
  461. X@var{v} is @samp{[10, 20, 30, 40]}, this calls @samp{(f (f (f 10 20) 30) 40)}.
  462. XIf @var{v} is a matrix, this reduces over the rows of @var{v}.
  463. X@end defun
  464. X
  465. X@defun reduce-cols f m
  466. XReduce the function @var{f} over the columns of matrix @var{m}.  For
  467. Xexample, if @var{m} is @samp{[[1, 2], [3, 4], [5, 6]]}, the result
  468. Xis a vector of the two elements @samp{(f (f 1 3) 5)} and @samp{(f (f 2 4) 6)}.
  469. X@end defun
  470. X
  471. X@defun mat-row m n
  472. XReturn the @var{n}th row of matrix @var{m}.  This is equivalent to
  473. X@samp{(elt m n)}.  For a slower but safer version, use @code{mrow}.
  474. X(@xref{Extracting Elements}.)
  475. X@end defun
  476. X
  477. X@defun mat-col m n
  478. XReturn the @var{n}th column of matrix @var{m}, in the form of a vector.
  479. XThe arguments are not checked for correctness.
  480. X@end defun
  481. X
  482. X@defun mat-less-row m n
  483. XReturn a copy of matrix @var{m} with its @var{n}th row deleted.  The
  484. Xnumber @var{n} must be in range from 1 to the number of rows in @var{m}.
  485. X@end defun
  486. X
  487. X@defun mat-less-col m n
  488. XReturn a copy of matrix @var{m} with its @var{n}th column deleted.
  489. X@end defun
  490. X
  491. X@defun transpose m
  492. XReturn the transpose of matrix @var{m}.
  493. X@end defun
  494. X
  495. X@defun flatten-vector v
  496. XFlatten nested vector @var{v} into a vector of scalars.  For example,
  497. Xif @var{v} is @samp{[[1, 2, 3], [4, 5]]} the result is @samp{[1, 2, 3, 4, 5]}.
  498. X@end defun
  499. X
  500. X@defun copy-matrix m
  501. XIf @var{m} is a matrix, return a copy of @var{m}.  This maps
  502. X@code{copy-sequence} over the rows of @var{m}; in Lisp terms, each
  503. Xelement of the result matrix will be @code{eq} to the corresponding
  504. Xelement of @var{m}, but none of the @code{cons} cells that make up
  505. Xthe structure of the matrix will be @code{eq}.  If @var{m} is a plain
  506. Xvector, this is the same as @code{copy-sequence}.@refill
  507. X@end defun
  508. X
  509. X@defun swap-rows m r1 r2
  510. XExchange rows @var{r1} and @var{r2} of matrix @var{m} in-place.  In
  511. Xother words, unlike most of the other functions described here, this
  512. Xfunction changes @var{m} itself rather than building up a new result
  513. Xmatrix.  The return value is @var{m}, i.e., @samp{(eq (swap-rows m 1 2) m)}
  514. Xis true, with the side effect of exchanging the first two rows of
  515. X@var{m}.@refill
  516. X@end defun
  517. X
  518. X@node Symbolic Lisp Functions, Formatting Lisp Functions, Vector Lisp Functions, Internals
  519. X@subsubsection Symbolic Functions
  520. X
  521. XThe functions described here operate on symbolic formulas in the
  522. XCalculator.
  523. X
  524. X@defun simplify expr
  525. XSimplify the expression @var{expr} by applying various algebraic rules.
  526. XThis is what the @kbd{a s} (@code{calc-simplify}) command uses.
  527. X@end defun
  528. X
  529. X@defun simplify-extended expr
  530. XSimplify the expression @var{expr}, with additional rules enabled that
  531. Xhelp do a more thorough job, while not being entirely ``safe'' in all
  532. Xcircumstances.  (For example, this mode will simplify @samp{sqrt(x^2)}
  533. Xto @samp{x}, which is only valid when @var{x} is positive.)  This is
  534. Ximplemented by temporarily binding the variable @code{math-living-dangerously}
  535. Xto @code{t} (using a @code{let} form) and calling @code{simplify}.
  536. XDangerous simplification rules are written to check this variable
  537. Xbefore taking any action.@refill
  538. X@end defun
  539. X
  540. X@defun simplify-units expr
  541. XSimplify the expression @var{expr}, treating variable names as units
  542. Xwhenever possible.  This works by binding the variable
  543. X@code{math-simplifying-units} to @code{t} while calling @code{simplify}.
  544. X@end defun
  545. X
  546. X@defmac math-defsimplify funcs body
  547. XRegister a new simplification rule; this is normally called as a top-level
  548. Xform, like @code{defun} or @code{defmath}.  If @var{funcs} is a symbol
  549. X(like @code{+} or @code{calcFunc-sqrt}), this simplification rule is
  550. Xapplied to the formulas which are calls to the specified function.  Or,
  551. X@var{funcs} can be a list of such symbols; the rule applies to all
  552. Xfunctions on the list.  The @var{body} is written like the body of a
  553. Xfunction with a single argument called @code{expr}.  The body will be
  554. Xexecuted with @code{expr} bound to a formula which is a call to one of
  555. Xthe functions @var{funcs}.  If the function body returns @code{nil}, or
  556. Xif it returns a result @code{equal} to the original @code{expr}, it is
  557. Xignored and Calc goes on to try the next simplification rule that applies.
  558. XIf the function body returns something different, that new formula is
  559. Xsubstituted for @var{expr} in the original formula.  The simplifier
  560. Xmakes multiple passes until no rules produce any further change.  The
  561. Xsimplifier calls @code{normalize} after every pass.@refill
  562. X
  563. XNote that, since @code{defmath} is not being used here, @var{body} must
  564. Xbe written in true Lisp code without the conveniences that @code{defmath}
  565. Xprovides.  If you prefer, you can have @var{body} simply call another
  566. Xfunction (defined with @code{defmath}) which does the real work.
  567. X
  568. XThe arguments of a function call will already have been simplified
  569. Xbefore any rules for the call itself are invoked.  Since a new argument
  570. Xlist is consed up when this happens, this means that the rule's body is
  571. Xallowed to rearrange the function's arguments destructively if that is
  572. Xconvenient.  Here is a typical example of a simplification rule:
  573. X
  574. X@example
  575. X(math-defsimplify calcFunc-sin
  576. X  (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
  577. X           (nth 1 (nth 1 expr)))
  578. X      (and math-living-dangerously
  579. X           (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
  580. X           (list 'calcFunc-sqrt
  581. X                 (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))))))
  582. X@end example
  583. X
  584. XThis is really a pair of rules written with one @code{math-defsimplify}
  585. Xfor convenience; the first replaces @samp{sin(arcsin(x))} with @samp{x},
  586. Xand the second, which is not safe for all @samp{x}, replaces
  587. X@samp{sin(arccos(x))} with @samp{sqrt(1-x^2)}.  Note that a @code{sqrt}
  588. Xformula is built rather than simply calling @samp{(sqrt @dots{})} to
  589. Xavoid problems with, for example, @samp{(sqrt 2)} aborting the computation
  590. Xif Symbolic Mode is enabled.  Since @code{normalize} is called after every
  591. Xsimplification pass, this @code{sqrt} formula will have a chance to be
  592. Xevaluated before the user sees it.@refill
  593. X@end defmac
  594. X
  595. X@defun common-constant-factor expr
  596. XCheck @var{expr} to see if it is a sum of terms all multiplied by the
  597. Xsame rational value.  If so, return this value.  If not, return @code{nil}.
  598. XFor example, if called on @samp{6x + 9y + 12z}, it would return 3, since
  599. X3 is a common factor of all the terms.
  600. X@end defun
  601. X
  602. X@defun cancel-common-factor expr factor
  603. XAssuming @var{expr} is a sum with @var{factor} as a common factor,
  604. Xdivide each term of the sum by @var{factor}.  This is done by
  605. Xdestructively modifying parts of @var{expr}, on the assumption that
  606. Xit is being used by a simplification rule (where such things are
  607. Xallowed; see above).  For example, consider this built-in rule for
  608. Xsquare roots:
  609. X
  610. X@example
  611. X(math-defsimplify calcFunc-sqrt
  612. X  (let ((fac (math-common-constant-factor (nth 1 expr))))
  613. X    (and fac
  614. X         (math-mul (list 'calcFunc-sqrt fac)
  615. X                   (list 'calcFunc-sqrt
  616. X                         (math-cancel-common-factor
  617. X                          (nth 1 expr) fac))))))
  618. X@end example
  619. X@end defun
  620. X
  621. X@defun frac-gcd a b
  622. XCompute a ``rational GCD'' of @var{a} and @var{b}, which must both be
  623. Xrational numbers.  This is the fraction composed of the GCD of the
  624. Xnumerators of @var{a} and @var{b}, over the GCD of the denominators.
  625. XIt is used by @code{common-constant-factor}.@refill
  626. X@end defun
  627. X
  628. X@defun map-tree func expr many
  629. XTry applying Lisp function @var{func} to various sub-expressions of
  630. X@var{expr}.  Initially, call @var{func} with @var{expr} itself as an
  631. Xargument.  If this returns an expression which is not @code{equal} to
  632. X@var{expr}, apply @var{func} again until eventually it does return
  633. X@var{expr} with no changes.  Then, if @var{expr} is a function call,
  634. Xrecursively apply @var{func} to each of the arguments.  This keeps going
  635. Xuntil no changes occur anywhere in the expression; this final expression
  636. Xis returned by @code{map-tree}.  Note that, unlike simplification rules,
  637. X@var{func} functions may @emph{not} make destructive changes to
  638. X@var{expr}.  If a third argument @var{many} is provided, it is an
  639. Xinteger which says how many times @var{func} may be applied; the
  640. Xdefault, as described above, is infinitely many times.@refill
  641. X@end defun
  642. X
  643. X@defun apply-rewrite expr old new cond
  644. XApply a rewrite rule at the top level of @var{expr}, if possible, and
  645. Xreturn the rewritten expression.  If the rule does not match, return
  646. X@code{nil}.
  647. X@end defun
  648. X
  649. X@defun check-rewrite-rules rules
  650. XMake sure @var{rules} is a Calc expression in a form suitable for use
  651. Xas rewrite rules (i.e., a vector of two or three elements, a vector
  652. Xof such vectors, or a variable whose definition is a valid set of
  653. Xrewrite rules).  If so, return it in the form of a Lisp list of rewrite
  654. Xrules.  If it is not valid, call @code{error} to abort the command.
  655. X@end defun
  656. X
  657. X@defun apply-rewrite-rules expr rules
  658. XApply a Lisp list of rewrite rules at the top level of an expression.
  659. XUse the first rule that applies; if none apply, return @code{nil}.
  660. X@end defun
  661. X
  662. X@defun deriv expr var value symb
  663. XCompute the derivative of @var{expr} with respect to variable @var{var}
  664. X(which may actually be any sub-expression).  If @var{value} is specified,
  665. Xthe derivative is evaluated at the value of @var{var}; otherwise, the
  666. Xderivative is left in terms of @var{var}.  If the expression contains
  667. Xfunctions for which no derivative formula is known, new derivative
  668. Xfunctions are invented by adding primes to the names; @pxref{Calculus}.
  669. XHowever, if @var{symb} is non-@code{nil}, the presence of undifferentiable
  670. Xfunctions in @var{expr} instead cancels the whole differentiation, and
  671. X@code{deriv} returns @code{nil} instead.
  672. X
  673. XDerivatives of an function of one argument can be defined by
  674. Xadding a @code{math-derivative} property to the function's property
  675. Xlist.  The value of the property should be a Lisp function of one
  676. Xargument (which is the argument that appears in the function call being
  677. Xdifferentiated), which should return a formula for the derivative.
  678. XFor example, the derivative of @code{ln} is defined by
  679. X
  680. X@example
  681. X(put 'calcFunc-ln 'math-derivative
  682. X     (function (lambda (u) (math-div 1 u))))
  683. X@end example
  684. X@end defun
  685. X
  686. X@defun tderiv expr var value symb
  687. XCompute the total derivative of @var{expr}.  This is the same as
  688. X@code{deriv}, except that variables other than @var{var} are not
  689. Xassumed to be constant with respect to @var{var}.
  690. X@end defun
  691. X
  692. X@defun integ expr var low high
  693. XCompute the integral of @var{expr} with respect to @var{var}.
  694. X@xref{Calculus}, for further details.
  695. X@end defun
  696. X
  697. X@defmac math-defintegral funcs body
  698. XDefine a rule for integrating a function or functions of one argument;
  699. Xthis macro is very similar in format to @code{math-defsimplify}.
  700. XThe main difference is that here @var{body} is the body of a function
  701. Xwith a single argument @code{u} which is bound to the argument to the
  702. Xfunction being integrated, not the function call itself.  Also, the
  703. Xvariable of integration is available as @code{math-integ-var}.  If
  704. Xevaluation of the integral requires doing further integrals, the body
  705. Xshould call @samp{(math-integral @var{x})} to find the integral of
  706. X@var{x} with respect to @code{math-integ-var}; this function returns
  707. X@code{nil} if the integral could not be done.  Some examples:
  708. X
  709. X@example
  710. X(math-defintegral calcFunc-conj
  711. X  (let ((int (math-integral u)))
  712. X    (and int
  713. X         (list 'calcFunc-conj int))))
  714. X
  715. X(math-defintegral calcFunc-cos
  716. X  (and (equal u math-integ-var)
  717. X       (math-from-radians-2 (list 'calcFunc-sin u))))
  718. X@end example
  719. X
  720. XIn the @code{cos} example, we define only the integral of @samp{cos(x) dx},
  721. Xrelying on the general integration-by-substitution facility to handle
  722. Xcosines of more complicated arguments.  An integration rule should return
  723. X@code{nil} if it can't do the integral; if several rules are defined for
  724. Xthe same function, they are tried in order until one returns a non-@code{nil}
  725. Xresult.@refill
  726. X@end defmac
  727. X
  728. X@defmac math-defintegral-2 funcs body
  729. XDefine a rule for integrating a function or functions of two arguments.
  730. XThis is exactly analogous to @code{math-defintegral}, except that @var{body}
  731. Xis written as the body of a function with two arguments, @var{u} and
  732. X@var{v}.@refill
  733. X@end defmac
  734. X
  735. X@defun solve-for lhs rhs var full
  736. XAttempt to solve the equation @samp{@var{lhs} = @var{rhs}} by isolating
  737. Xthe variable @var{var} on the lefthand side; return the resulting righthand
  738. Xside, or @code{nil} if the equation cannot be solved.  The variable
  739. X@var{var} must appear at least once in @var{lhs} or @var{rhs}; if it
  740. Xappears more than once, Calc can solve the equation in only a few cases
  741. X(such as when the quadratic formula can be applied).  Note that the
  742. Xreturn value is a formula which does not contain @var{var}; this is
  743. Xdifferent from the user-level @code{solve} and @code{finv} functions,
  744. Xwhich return a rearranged equation or a functional inverse, respectively.
  745. XIf @var{full} is non-@code{nil}, a full solution including dummy signs
  746. Xand dummy integers will be produced.  User-defined inverses are provided
  747. Xas properties in a manner similar to derivatives:@refill
  748. X
  749. X@example
  750. X(put 'calcFunc-ln 'math-inverse
  751. X     (function (lambda (x) (list 'calcFunc-exp x))))
  752. X@end example
  753. X
  754. XThis function can call @samp{(math-solve-get-sign @var{x})} to create
  755. Xa new arbitrary sign variable, returning @var{x} times that sign, and
  756. X@samp{(math-solve-get-int @var{x})} to create a new arbitrary integer
  757. Xvariable multiplied by @var{x}.  These functions simply return @var{x}
  758. Xif the caller requested a non-``full'' solution.
  759. X@end defun
  760. X
  761. X@defun expr-contains expr var
  762. XReturns the number of occurrences of @var{var} as a subexpression
  763. Xof @var{expr}, or @code{nil} if there are no occurrences.  Thus,
  764. Xthis function can be used as a Lisp predicate or as an actual counting
  765. Xfunction.@refill
  766. X@end defun
  767. X
  768. X@defun expr-depends expr var
  769. XReturns true if @var{expr} refers to any variable the occurs in @var{var}.
  770. XIn other words, it checks if @var{expr} and @var{var} have any variables
  771. Xin common.
  772. X@end defun
  773. X
  774. X@defun expr-contains-vars expr
  775. XReturn true if @var{expr} contains any variables, or @code{nil} if @var{expr}
  776. Xcontains only constants and functions with constant arguments.
  777. X@end defun
  778. X
  779. X@defun expr-subst expr old new
  780. XReturns a copy of @var{expr}, with all occurrences of @var{old} replaced
  781. Xby @var{new}.  This treats the derivative forms specially with respect
  782. Xto the dummy variable, so that the effect is always to return @var{expr}
  783. Xevaluated at @var{old} = @var{new}.@refill
  784. X@end defun
  785. X
  786. X@defun expr-weight expr
  787. XReturns the ``weight'' of @var{expr}, basically a count of the total
  788. Xnumber of objects and function calls that appear in @var{expr}.  For
  789. X``primitive'' objects, this will be one.
  790. X@end defun
  791. X
  792. X@defun expr-height expr
  793. XReturns the ``height'' of @var{expr}, which is the deepest level to
  794. Xwhich function calls are nested.  (Note that @samp{@var{a} + @var{b}}
  795. Xcounts as a function call.)  For primitive objects, this returns zero.@refill
  796. X@end defun
  797. X
  798. X@defun polynomial-p expr var
  799. XCheck if @var{expr} is a polynomial in variable (or sub-expression)
  800. X@var{var}.  If so, return the degree of the polynomial, that is, the
  801. Xhighest power of @var{var} that appears in @var{expr}.  For example,
  802. Xfor @samp{(x^2 + 3)^3 + 4} this would return 6.  This function returns
  803. X@code{nil} unless @var{expr}, when expanded out by @kbd{a x}
  804. X(@code{calc-expand}), would consist of a sum of terms in which @var{var}
  805. Xappears only raised to nonnegative integer powers.  Note that if
  806. X@var{var} does not occur in @var{expr}, then @var{expr} is considered
  807. Xa polynomial of degree 0.@refill
  808. X@end defun
  809. X
  810. X@defun is-polynomial expr var degree loose
  811. XCheck if @var{expr} is a polynomial in variable or sub-expression
  812. X@var{var}, and, if so, return a list representation of the polynomial
  813. Xwhere the elements of the list are coefficients of successive powers of
  814. X@var{var}: @samp{@var{a} + @var{b} x + @var{c} x^3} would produce the
  815. Xlist @samp{(@var{a} @var{b} 0 @var{c})}, and @samp{(x + 1)^2} would
  816. Xproduce the list @samp{(1 2 1)}.  The highest element of the list will
  817. Xbe non-zero, with the special exception that if @var{expr} is the
  818. Xconstant zero, the returned value will be @samp{(0)}.  Return @code{nil}
  819. Xif @var{expr} is not a polynomial in @var{var}.  If @var{degree} is
  820. Xspecified, this will not consider polynomials of degree higher than that
  821. Xvalue.  This is a good precaution because otherwise an input of
  822. X@samp{(x+1)^1000} will cause a huge coefficient list to be built.  If
  823. X@var{loose} is non-@code{nil}, then a looser definition of a polynomial
  824. Xis used in which coefficients are no longer required not to depend on
  825. X@var{var}, but are only required not to take the form of polynomials
  826. Xthemselves.  For example, @samp{sin(x) x^2 + cos(x)} is a loose
  827. Xpolynomial with coefficients @samp{((calcFunc-cos x) 0 (calcFunc-sin
  828. Xx))}.  The result will never be @code{nil} in loose mode, since any
  829. Xexpression can be interpreted as a ``constant'' loose polynomial.@refill
  830. X@end defun
  831. X
  832. X@defun polynomial-base expr pred
  833. XCheck if @var{expr} is a polynomial in any variable that occurs in it;
  834. Xif so, return that variable.  (If @var{expr} is a multivariate polynomial,
  835. Xchoose one variable arbitrarily.)  If @var{pred} is specified, it should
  836. Xbe a Lisp function which is called as @samp{(@var{pred} @var{subexpr})},
  837. Xand which should return true if @code{mpb-top-expr} (a global name for
  838. Xthe original @var{expr}) is a suitable polynomial in @var{subexpr}.
  839. XThe default predicate uses @samp{(polynomial-p mpb-top-expr @var{subexpr})};
  840. Xyou can use @var{pred} to specify additional conditions.  Or, you could
  841. Xhave @var{pred} build up a list of every suitable @var{subexpr} that
  842. Xis found.@refill
  843. X@end defun
  844. X
  845. X@defun poly-simplify poly
  846. XSimplify polynomial coefficient list @var{poly} by (destructively)
  847. Xclipping off trailing zeros.
  848. X@end defun
  849. X
  850. X@defun poly-mix a ac b bc
  851. XMix two polynomial lists @var{a} and @var{b} (in the form returned by
  852. X@code{is-polynomial}) in a linear combination with coefficient expressions
  853. X@var{ac} and @var{bc}.  The result is a (not necessarily simplified)
  854. Xpolynomial list representing @samp{@var{ac} @var{a} + @var{bc} @var{b}}.@refill
  855. X@end defun
  856. X
  857. X@defun poly-mul a b
  858. XMultiply two polynomial coefficient lists @var{a} and @var{b}.  The
  859. Xresult will be in simplified form if the inputs were simplified.
  860. X@end defun
  861. X
  862. X@defun build-polynomial-expr poly var
  863. XConstruct a Calc formula which represents the polynomial coefficient
  864. Xlist @var{poly} applied to variable @var{var}.  The @kbd{a c}
  865. X(@code{calc-collect}) command uses @code{is-polynomial} to turn an
  866. Xexpression into a coefficient list, then @code{build-polynomial-expr}
  867. Xto turn the list back into an expression in regular form.@refill
  868. X@end defun
  869. X
  870. X@defun check-unit-name var
  871. XCheck if @var{var} is a variable which can be interpreted as a unit
  872. Xname.  If so, return the units table entry for that unit.  This
  873. Xwill be a list whose first element is the unit name (not counting
  874. Xprefix characters) as a symbol and whose second element is the
  875. XCalc expression which defines the unit.  (Refer to the Calc sources
  876. Xfor details on the remaining elements of this list.)  If @var{var}
  877. Xis not a variable or is not a unit name, return @code{nil}.
  878. X@end defun
  879. X
  880. X@defun units-in-expr-p expr sub-exprs
  881. XReturn true if @var{expr} contains any variables which can be
  882. Xinterpreted as units.  If @var{sub-exprs} is @code{t}, the entire
  883. Xexpression is searched.  If @var{sub-exprs} is @code{nil}, this
  884. Xchecks whether @var{expr} is directly a units expression.@refill
  885. X@end defun
  886. X
  887. X@defun single-units-in-expr-p expr
  888. XCheck whether @var{expr} contains exactly one units variable.  If so,
  889. Xreturn the units table entry for the variable.  If @var{expr} does
  890. Xnot contain any units, return @code{nil}.  If @var{expr} contains
  891. Xtwo or more units, return the symbol @samp{'wrong}.
  892. X@end defun
  893. X
  894. X@defun to-standard-units expr which
  895. XConvert units expression @var{expr} to base units.  If @var{which}
  896. Xis @code{nil}, use Calc's native base units.  Otherwise, @var{which}
  897. Xcan specify a units system, which is a list of two-element lists,
  898. Xwhere the first element is a Calc base symbol name and the second
  899. Xis an expression to substitute for it.@refill
  900. X@end defun
  901. X
  902. X@defun remove-units expr
  903. XReturn a copy of @var{expr} with all units variables replaced by ones.
  904. XThis expression is generally normalized before use.
  905. X@end defun
  906. X
  907. X@defun extract-units expr
  908. XReturn a copy of @var{expr} with everything but units variables replaced
  909. Xby ones.
  910. X@end defun
  911. X
  912. X@node Formatting Lisp Functions, Lisp Variables, Symbolic Lisp Functions, Internals
  913. X@subsubsection I/O and Formatting Functions
  914. X
  915. XThe functions described here are responsible for parsing and formatting
  916. XCalc numbers and formulas.
  917. X
  918. X@defun read-number str
  919. XIf string @var{str} contains a valid Calc number, either integer,
  920. Xfraction, float, or HMS form, this function parses and returns that
  921. Xnumber.  Otherwise, it returns @code{nil}.
  922. X@end defun
  923. X
  924. X@defun read-expr str
  925. XRead an algebraic expression from string @var{str}.  If @var{str} does
  926. Xnot have the form of a valid expression, return a list of the form
  927. X@samp{(error @var{pos} @var{msg})} where @var{pos} is an integer index
  928. Xinto @var{str} of the general location of the error, and @var{msg} is
  929. Xa string describing the problem.@refill
  930. X@end defun
  931. X
  932. X@defun read-exprs str
  933. XRead a list of expressions separated by commas, and return it as a
  934. XLisp list.  If an error occurs in any expressions, an error list as
  935. Xshown above is returned instead.
  936. X@end defun
  937. X
  938. X@defun calc-do-alg-entry initial prompt no-norm
  939. XRead an algebraic formula or formulas using the minibuffer.  All
  940. Xconventions of regular algebraic entry are observed.  The return value
  941. Xis a list of Calc formulas; there will be more than one if the user
  942. Xentered a list of values separated by commas.  The result is @code{nil}
  943. Xif the user presses Return with a blank line.  If @var{initial} is
  944. Xgiven, it is a string which the minibuffer will initially contain.
  945. XIf @var{prompt} is given, it is the prompt string to use; the default
  946. Xis ``Algebraic:''.  If @var{no-norm} is @code{t}, the formulas will
  947. Xbe returned exactly as parsed; otherwise, they will be passed through
  948. X@code{calc-normalize} first.@refill
  949. X
  950. XTo support the use of @kbd{$} characters in the algebraic entry, use
  951. X@code{let} to bind @code{calc-dollar-values} to a list of the values
  952. Xto be substituted for @kbd{$}, @kbd{$$}, and so on, and bind
  953. X@code{calc-dollar-used} to 0.  Upon return, @code{calc-dollar-used}
  954. Xwill have been changed to the highest number of consecutive @kbd{$}s
  955. Xthat actually appeared in the input.@refill
  956. X@end defun
  957. X
  958. X@defun format-number a
  959. XConvert the real or complex number or HMS form @var{a} to string form.
  960. X@end defun
  961. X
  962. X@defun format-flat-expr a prec
  963. XConvert the arbitrary Calc number or formula @var{a} to string form,
  964. Xin the style used by the trail buffer.  This is a simple format designed
  965. Xmostly to guarantee the string is of a form that can be re-parsed by
  966. X@code{read-expr}.  Most formatting modes, such as digit grouping,
  967. Xcomplex number format, and point character, are ignored to ensure the
  968. Xresult will be re-readable.  The @var{prec} parameter is normally 0; if
  969. Xyou pass a large integer like 1000 instead, the expression will be
  970. Xsurrounded by parentheses unless it is a plain number or variable name.
  971. X@end defun
  972. X
  973. X@defun format-value a width
  974. XConvert the Calc number or formula @var{a} to string form, using the
  975. Xformat seen in the stack buffer.  Beware the the string returned may
  976. Xnot be re-readable by @code{read-expr}, for example, because of digit
  977. Xgrouping.  Multi-line objects like matrices produce strings that
  978. Xcontain newline characters to separate the lines.  The @var{w}
  979. Xparameter, if given, is the target window size for which to format
  980. Xthe expressions.  If @var{w} is omitted, the width of the Calculator
  981. Xwindow is used.@refill
  982. X@end defun
  983. X
  984. X@defun compose-expr a prec
  985. XFormat the Calc number or formula @var{a} according to the current
  986. Xlanguage mode, returning a ``composition.''  To learn about the
  987. Xstructure of compositions, see the comments in the Calc source code.
  988. XIn the ``big'' language mode, you can specify the format of a given
  989. Xtype of function call by putting a @code{math-compose-big} property
  990. Xon the function's symbol, whose value is a Lisp function that takes
  991. X@var{a} and @var{prec} as arguments and returns a composition.
  992. X@end defun
  993. X
  994. X@defun composition-to-string c w
  995. XConvert a composition structure returned by @code{compose-expr} into
  996. Xa string.  Multi-line compositions convert to strings containing
  997. Xnewline characters.  The target window size is given by @var{w}.
  998. XThe @code{format-value} function basically calls @code{compose-expr}
  999. Xfollowed by @code{composition-to-string}.
  1000. X@end defun
  1001. X
  1002. X@defun comp-width c
  1003. XCompute the width in characters of composition @var{c}.
  1004. X@end defun
  1005. X
  1006. X@defun comp-height c
  1007. XCompute the height in lines of composition @var{c}.
  1008. X@end defun
  1009. X
  1010. X@defun comp-ascent c
  1011. XCompute the portion of the height of composition @var{c} which is on or
  1012. Xabove the baseline.  For a one-line composition, this will be one.
  1013. X@end defun
  1014. X
  1015. X@defun comp-descent c
  1016. XCompute the portion of the height of composition @var{c} which is below
  1017. Xthe baseline.  For a one-line composition, this will be zero.
  1018. X@end defun
  1019. X
  1020. X@defun comp-first-char c
  1021. XIf composition @var{c} is a simple horizontal composition, return the
  1022. Xfirst (leftmost) character of the composition as an integer.  Otherwise,
  1023. Xreturn @code{nil}.@refill
  1024. X@end defun
  1025. X
  1026. X@defun comp-last-char c
  1027. XIf composition @var{c} is a simple horizontal composition, return the
  1028. Xlast (rightmost) character, otherwise return @code{nil}.
  1029. X@end defun
  1030. X
  1031. X@node Lisp Variables, Hooks, Formatting Lisp Functions, Internals
  1032. X@subsubsection Lisp Variables
  1033. X
  1034. X(This section is currently unfinished.)
  1035. X
  1036. X@node Hooks, , Lisp Variables, Internals
  1037. X@subsubsection Hooks
  1038. X
  1039. X(This section is currently unfinished.)
  1040. X
  1041. X@node Installation, Reporting Bugs, Programming, Top
  1042. X@chapter Installation
  1043. X
  1044. XCalc comes as a pair of Emacs Lisp files, generally called
  1045. X@file{calc.el} and @file{calc-ext.el}.  The first contains the basic
  1046. Xfoundations of the Calculator, and is as small as possible to promote
  1047. Xquick loading.  The second contains all the more advanced commands and
  1048. Xfunctions.  Calc is usually installed so that the @kbd{M-x calc} or
  1049. X@kbd{M-#} command auto-loads only the first part, and the second part is
  1050. Xauto-loaded whenever the first advanced feature is used.@refill
  1051. X
  1052. XCalc is written in a way that maximizes performance when its code has been
  1053. Xbyte-compiled; a side effect is that performance is seriously degraded if
  1054. Xit @emph{isn't} compiled.  Thus, it is essential to compile the Calculator
  1055. Xbefore trying to use it.  The Emacs command @kbd{M-x byte-compile-file}
  1056. Xis used to compile an Emacs Lisp file.  Compile each of @file{calc.el} and
  1057. X@file{calc-ext.el} to obtain byte-code files @file{calc.elc} and
  1058. X@file{calc-ext.elc}.  You may find you need to do @kbd{M-x load-file
  1059. Xcalc.elc} before compiling @file{calc-ext.el} will work.
  1060. X
  1061. XFor your convenience, the FTP distribution of Calc, obtainable from
  1062. Xanonymous FTP on @samp{csvax.caltech.edu}, includes already-compiled
  1063. Xversions of both of these files.
  1064. X
  1065. XTo teach Emacs how to load in Calc when you type @kbd{M-x calc} for the
  1066. Xfirst time, include these lines in your @file{.emacs} file (if you are
  1067. Xinstalling Calc just for your own use), or the system's @file{lisp/default}
  1068. Xfile (if you are installing Calc publicly).
  1069. X
  1070. X@example
  1071. X(autoload 'calc             ".../calc.elc"     "Calculator Mode" t nil)
  1072. X(autoload 'calc-extensions  ".../calc-ext.elc" nil nil nil)
  1073. X(autoload 'quick-calc       ".../calc.elc"     "Quick Calculator" t nil)
  1074. X(autoload 'calc-grab-region ".../calc-ext.elc" nil t nil)
  1075. X(autoload 'defmath          ".../calc-ext.elc" nil t t)
  1076. X@end example
  1077. X
  1078. Xwhere @file{.../calc.elc} represents the full path to the @file{calc.elc}
  1079. Xfile, and similarly for @file{.../calc-ext.elc}.  If you have installed
  1080. Xthese files in Emacs' main @file{lisp/} directory, you can just write
  1081. X@samp{"calc.elc"} and @samp{"calc-ext.elc"}.
  1082. X
  1083. XThe @code{autoload} command for @code{calc} is what loads @file{calc.elc}
  1084. Xwhen you type @kbd{M-x calc}.  The @code{autoload} for @code{calc-extensions}
  1085. Xbrings in the extensions module; Calc takes care to call the
  1086. X@code{calc-extensions} function (which doesn't actually do anything)
  1087. Xbefore any operation that requires the extensions to be present.
  1088. XThe other three @code{autoload} commands are for functions which might
  1089. Xreasonably be used before the user has typed @kbd{M-x calc} for the
  1090. Xfirst time.
  1091. X
  1092. XIf you don't want to bother with a split Calculator, you can simply
  1093. Xconcatenate @code{calc-ext.elc} onto the end of @code{calc.elc}, rewrite
  1094. Xthe above @code{autoload} commands all to point to the combined file,
  1095. Xand treat Calc as one big program.  You may need to do this if
  1096. X@code{autoload} is giving you problems.
  1097. X
  1098. XYou may also wish to bind the @code{calc} command to a key.  The
  1099. Xrecommended keystroke is @kbd{M-#} (i.e., Meta-Shift-3).  To set up
  1100. Xthis key binding, include this command in your @file{.emacs} or
  1101. X@file{lisp/default} file:
  1102. X
  1103. X@example
  1104. X(global-set-key "\e#" 'calc)
  1105. X@end example
  1106. X
  1107. XThere are no standard key assignments for @code{quick-calc} and
  1108. X@code{calc-grab-region}, but you may wish to define some.
  1109. X
  1110. XThe file @file{macedit.el} contains another useful Emacs extension
  1111. Xcalled @code{edit-kbd-macro}.  It allows you to edit a keyboard macro
  1112. Xin human-readable form.  The @kbd{Z E} command in Calc knows how to
  1113. Xuse it to edit user commands that have been defined by keyboard macros.
  1114. XTo autoload it, you will want to include the commands,
  1115. X
  1116. X@example
  1117. X(autoload 'edit-kbd-macro      ".../macedit.elc" "Edit Keyboard Macro" t nil)
  1118. X(autoload 'edit-last-kbd-macro ".../macedit.elc" "Edit Keyboard Macro" t nil)
  1119. X@end example
  1120. X
  1121. XThe documentation for Calc (i.e., this manual) comes in a file
  1122. X@file{calc.texinfo}.  To format this for use as an on-line manual,
  1123. Xopen this file for editing in Emacs and give the command
  1124. X@kbd{M-x texinfo-format-buffer}.  When this finishes, type @kbd{C-x C-s}
  1125. Xto save.  The result will be a collection of files whose names begin
  1126. Xwith @file{calc-info}.  You can also format this into a printable
  1127. Xdocument using @TeX{}, but beware, the manual is about 170 printed pages!
  1128. X
  1129. X@vindex calc-info-filename
  1130. XThere is a Lisp variable called @code{calc-info-filename} which holds
  1131. Xthe name of the Info file containing Calc's on-line documentation.
  1132. XIts default value is @samp{calc-info}, which will work correctly if
  1133. Xthe Info files are stored in Emacs' main @file{info/} directory.  If
  1134. Xyou keep them elsewhere, you will want to put a command of the form,
  1135. X
  1136. X@example
  1137. X(setq calc-info-filename ".../calc-info")
  1138. X@end example
  1139. X
  1140. X@noindent
  1141. Xin your @file{.emacs} or @file{lisp/default} file, where again @file{...}
  1142. Xrepresents the directory containing the Info files.
  1143. X
  1144. X@vindex calc-settings-file
  1145. XAnother variable you might want to set is @code{calc-settings-file},
  1146. Xwhich holds the file name in which commands like @kbd{m m} and @kbd{Z P}
  1147. Xstore ``permanent'' definitions.  The default value for this variable
  1148. Xis @samp{"~/.emacs"}.  If @code{calc-settings-file} does not contain
  1149. X@samp{".emacs"} as a substring, and if the variable
  1150. X@code{calc-loaded-settings-file} is @code{nil}, then Calc will
  1151. Xautomatically load your settings file (if it exists) the first time
  1152. XCalc is invoked.@refill
  1153. X
  1154. XTo test your installation of Calc, start a fresh Emacs and type @kbd{M-#}
  1155. Xto make sure the autoload commands and key bindings work.  Now, type
  1156. X@kbd{i} to make sure Calc can find its Info documentation.  Press @kbd{q}
  1157. Xto exit the Info system.  Type @kbd{20 S} to compute the sine of
  1158. X20 degrees; this will test the autoloading of the extensions module.
  1159. XThe result should be 0.342020143326.  Finally, press @kbd{M-#} again to
  1160. Xmake sure the Calculator can exit.
  1161. X
  1162. X(The above text is included in both the Calc documentation and the
  1163. Xfile INSTALL in the Calc distribution directory.)
  1164. X
  1165. X@node Reporting Bugs, Key Index, Installation, Top
  1166. X@chapter Reporting Bugs
  1167. X
  1168. XIf you find a bug in Calc, send e-mail to Dave Gillespie,
  1169. X@samp{daveg@@csvax.caltech.edu}.  While I cannot guarantee that I
  1170. Xwill have time to work on your bug, I do try to fix bugs quickly
  1171. Xwhenever I can.
  1172. X
  1173. XThere is an automatic @kbd{M-x report-calc-bug} command which helps
  1174. Xyou to report bugs.  This command prompts you for a brief subject
  1175. Xline, then leaves you in a mail editing buffer.  Type @kbd{C-c C-s} to
  1176. Xsend your mail.  Make sure your subject line indicates that you are
  1177. Xreporting a Calc bug; this command sends mail to my regular mailbox.
  1178. X
  1179. XIf you have suggestions for additional features for Calc, I would
  1180. Xlove to hear them.  Some have dared to suggest that Calc is already
  1181. Xtop-heavy with features; I really don't see what they're talking
  1182. Xabout, so, if you have ideas, send them right in.  (I may even have
  1183. Xtime to implement them!)
  1184. X
  1185. XAt the front of the source file, @file{calc.el}, is a list of ideas for
  1186. Xfuture work which I have not had time to do.  If any enthusiastic souls
  1187. Xwish to take it upon themselves to work on these, I would be delighted.
  1188. XPlease let me know if you plan to contribute to Calc so I can coordinate
  1189. Xyour efforts with mine and those of others.  I will do my best to help
  1190. Xyou in whatever way I can.
  1191. X
  1192. X@node Key Index, Command Index, Reporting Bugs, Top
  1193. X@unnumbered Index of Key Sequences
  1194. X
  1195. X@printindex ky
  1196. X
  1197. X@node Command Index, Function Index, Key Index, Top
  1198. X@unnumbered Index of Calculator Commands
  1199. X
  1200. XSince all Calculator commands begin with the prefix @samp{calc-}, the
  1201. X@kbd{x} key has been provided as a variant of @kbd{M-x} which automatically
  1202. Xtypes @samp{calc-} for you.  Thus, @kbd{x last-x} is short for
  1203. X@kbd{M-x calc-last-x}.
  1204. X
  1205. X@printindex pg
  1206. X
  1207. X@node Function Index, Concept Index, Command Index, Top
  1208. X@unnumbered Index of Algebraic Functions
  1209. X
  1210. XThis is a list of built-in functions usable in algebraic expressions.
  1211. XTheir full Lisp names are derived by adding the prefix @samp{calcFunc-},
  1212. Xas in @samp{calcFunc-sqrt}.
  1213. X
  1214. X@printindex tp
  1215. X
  1216. X@node Concept Index, Lisp Function Index, Function Index, Top
  1217. X@unnumbered Concept Index
  1218. X
  1219. X@printindex cp
  1220. X
  1221. X@node Lisp Function Index, Lisp Variable Index, Concept Index, Top
  1222. X@unnumbered Index of Lisp Math Functions
  1223. X
  1224. XThe following functions are meant to be used with @code{defmath}, not
  1225. X@code{defun} definitions.  For names that do not start with @samp{calc-},
  1226. Xthe corresponding full Lisp name is derived by adding a prefix of
  1227. X@samp{math-}.
  1228. X
  1229. X@printindex fn
  1230. X
  1231. X@node Lisp Variable Index, , Lisp Function Index, Top
  1232. X@unnumbered Index of Lisp Variables
  1233. X
  1234. X@printindex vr
  1235. X
  1236. X@summarycontents
  1237. X@contents
  1238. X@bye
  1239. X
  1240. X
  1241. SHAR_EOF
  1242. echo "File calc.texinfo is complete"
  1243. chmod 0664 calc.texinfo || echo "restore of calc.texinfo fails"
  1244. set `wc -c calc.texinfo`;Sum=$1
  1245. if test "$Sum" != "465844"
  1246. then echo original size 465844, current size $Sum;fi
  1247. rm -f s2_seq_.tmp
  1248. echo "You have unpacked the last part"
  1249. exit 0
  1250.